/*     */ package com.zimbra.qa.unittest.prov.ldap;
/*     */ 
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.account.ldap.LdapDIT;
/*     */ import com.zimbra.cs.account.ldap.LdapProv;
/*     */ import com.zimbra.cs.ldap.LdapClient;
/*     */ import com.zimbra.cs.ldap.LdapServerType;
/*     */ import com.zimbra.cs.ldap.LdapUsage;
/*     */ import com.zimbra.cs.ldap.ZLdapContext;
/*     */ import com.zimbra.cs.ldap.ZLdapFilter;
/*     */ import com.zimbra.cs.ldap.ZLdapFilterFactory;
/*     */ import com.zimbra.cs.ldap.ZSearchControls;
/*     */ import com.zimbra.cs.ldap.ZSearchResultEntry;
/*     */ import com.zimbra.cs.ldap.ZSearchResultEnumeration;
/*     */ import com.zimbra.cs.ldap.ZSearchScope;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashSet;
/*     */ import java.util.List;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class Cleanup
/*     */ {
/*     */   public static void deleteAll(String... domainNames)
/*     */     throws Exception
/*     */   {
/*  41 */     for (String domainName : domainNames) {
/*  42 */       deleteEntireBranch(domainName);
/*     */     }
/*  44 */     deleteAllNonDefaultCoses();
/*  45 */     deleteAllNonDefaultServers();
/*  46 */     deleteAllXMPPComponents();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private static void deleteEntireBranch(String domainName)
/*     */     throws Exception
/*     */   {
/*  54 */     String[] parts = domainName.split("\\.");
/*  55 */     String[] dns = ((LdapProv)Provisioning.getInstance()).getDIT().domainToDNs(parts);
/*  56 */     String topMostRDN = dns[(dns.length - 1)];
/*  57 */     deleteEntireBranchByDN(topMostRDN);
/*     */   }
/*     */   
/*     */   private static void deleteAllNonDefaultCoses() throws Exception
/*     */   {
/*  62 */     LdapDIT dit = ((LdapProv)Provisioning.getInstance()).getDIT();
/*  63 */     String cosBaseDN = dit.cosBaseDN();
/*     */     
/*  65 */     Set<String> defaultCosDN = new HashSet();
/*  66 */     defaultCosDN.add(dit.cosNametoDN("default"));
/*  67 */     defaultCosDN.add(dit.cosNametoDN("defaultExternal"));
/*     */     
/*  69 */     deleteAllChildrenUnderDN(cosBaseDN, defaultCosDN);
/*     */   }
/*     */   
/*     */   private static void deleteAllNonDefaultServers() throws Exception {
/*  73 */     LdapProv ldapProv = LdapProv.getInst();
/*  74 */     LdapDIT dit = ldapProv.getDIT();
/*  75 */     String serverBaseDN = dit.serverBaseDN();
/*     */     
/*  77 */     Set<String> defaultServerDN = new HashSet();
/*  78 */     defaultServerDN.add(dit.serverNameToDN(ldapProv.getLocalServer().getName()));
/*     */     
/*  80 */     deleteAllChildrenUnderDN(serverBaseDN, defaultServerDN);
/*     */   }
/*     */   
/*     */   private static void deleteAllXMPPComponents() throws Exception {
/*  84 */     String xmppBaseDN = ((LdapProv)Provisioning.getInstance()).getDIT().xmppcomponentBaseDN();
/*  85 */     deleteAllChildrenUnderDN(xmppBaseDN, null);
/*     */   }
/*     */   
/*     */   private static void deleteEntireBranchByDN(String dn)
/*     */     throws Exception
/*     */   {
/*  91 */     ZLdapContext zlc = null;
/*     */     try
/*     */     {
/*  94 */       zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.UNITTEST);
/*  95 */       deleteEntireBranch(zlc, dn);
/*     */     } finally {
/*  97 */       LdapClient.closeContext(zlc);
/*     */     }
/*     */   }
/*     */   
/*     */   private static void deleteAllChildrenUnderDN(String dn, Set<String> ignoreDNs)
/*     */     throws Exception
/*     */   {
/* 104 */     ZLdapContext zlc = null;
/*     */     try {
/* 106 */       zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.UNITTEST);
/* 107 */       List<String> childrenDNs = getDirectChildrenDNs(zlc, dn);
/* 108 */       for (String childDN : childrenDNs) {
/* 109 */         if ((ignoreDNs == null) || (!ignoreDNs.contains(childDN))) {
/* 110 */           deleteEntireBranch(zlc, childDN);
/*     */         }
/*     */       }
/*     */     } finally {
/* 114 */       LdapClient.closeContext(zlc);
/*     */     }
/*     */   }
/*     */   
/*     */   private static void deleteEntireBranch(ZLdapContext zlc, String dn)
/*     */     throws Exception
/*     */   {
/* 121 */     if (isLeaf(zlc, dn)) {
/* 122 */       deleteEntry(dn);
/* 123 */       return;
/*     */     }
/*     */     
/* 126 */     List<String> childrenDNs = getDirectChildrenDNs(zlc, dn);
/* 127 */     for (String childDN : childrenDNs) {
/* 128 */       deleteEntireBranch(zlc, childDN);
/*     */     }
/* 130 */     deleteEntry(dn);
/*     */   }
/*     */   
/*     */   private static void deleteEntry(String dn) throws Exception {
/* 134 */     ZLdapContext zlc = null;
/*     */     try {
/* 136 */       zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.UNITTEST);
/* 137 */       zlc.deleteEntry(dn);
/*     */     } finally {
/* 139 */       LdapClient.closeContext(zlc);
/*     */     }
/*     */   }
/*     */   
/*     */   private static boolean isLeaf(ZLdapContext zlc, String dn) throws Exception {
/* 144 */     return getDirectChildrenDNs(zlc, dn).size() == 0;
/*     */   }
/*     */   
/*     */   private static List<String> getDirectChildrenDNs(ZLdapContext zlc, String dn) throws Exception {
/* 148 */     List<String> childrenDNs = new ArrayList();
/*     */     
/* 150 */     ZLdapFilter filter = ZLdapFilterFactory.getInstance().anyEntry();
/*     */     
/* 152 */     ZSearchControls searchControls = ZSearchControls.createSearchControls(ZSearchScope.SEARCH_SCOPE_ONELEVEL, 0, new String[] { "objectClass" });
/*     */     
/*     */ 
/*     */ 
/* 156 */     ZSearchResultEnumeration sr = zlc.searchDir(dn, filter, searchControls);
/* 157 */     while (sr.hasMore()) {
/* 158 */       ZSearchResultEntry entry = sr.next();
/* 159 */       childrenDNs.add(entry.getDN());
/*     */     }
/* 161 */     sr.close();
/*     */     
/* 163 */     return childrenDNs;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/prov/ldap/Cleanup.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */